<template>
  <div class='goods-relevant' v-if="list">
    <div class="header">
      <i class="icon" />
      <span class="title">{{id ? '同类商品推荐' : '猜你喜欢'}}</span>
    </div>
    <!-- 此处使用改造后的xtx-carousel.vue -->
    <XtxCarousel :sliders="list"/>
  </div>
</template>

<script>
import { findProductList } from '@/api/product'
import { useRoute } from 'vue-router'
import { reactive, watch } from 'vue'
// import { watch } from 'vue'
export default {
  name: 'GoodsRelevant',
  setup () {
    let list = reactive([])
    const route = useRoute()
    const id = route.params.id
    // sliders = [[],[],[],[]]

    watch(() => id, (newVal, oldValue) => {
      findProductList({
        id,
        limit: 16
      }).then(data => {
      // [16个商品]
      // 将data.result数据赋值给list数据保证数据结构正确
        const pageSize = 4
        const pageCount = Math.ceil((data.result.length / pageSize))
        for (let i = 0; i < pageCount; i++) {
          list.push(data.result.slice(pageSize * i, pageSize * (i + 1)))
        }
        list = data.result
        // console.log(data)
      })
    }, { immediate: true })

    return { list, id }
  }
}
</script>

<style scoped lang='less'>
.goods-relevant {
  background: #fff;
  min-height: 460px;
  margin-top: 20px;
    .header {
    height: 80px;
    line-height: 80px;
    padding: 0 20px;
    .title {
      font-size: 20px;
      padding-left: 10px;
    }
    .icon {
      width: 16px;
      height: 16px;
      display: inline-block;
      border-top: 4px solid @xtxColor;
      border-right: 4px solid @xtxColor;
      box-sizing: border-box;
      position: relative;
      transform: rotate(45deg);
      &::before {
        content: "";
        width: 10px;
        height: 10px;
        position: absolute;
        display: inline-block;
        left: 0;
        top: 2px;
        background: lighten(@xtxColor, 40%);
      }
    }
  }
}
:deep(.xtx-carousel) {
  height: 380px;
  .carousel {
    &-indicator {
      bottom: 30px;
      span {
        &.active {
          background: @xtxColor;
        }
      }
    }
    &-btn {
      top: 110px;
      opacity: 1;
      background: rgba(0,0,0,0);
      color: #ddd;
      i {
        font-size: 30px;
      }
    }
  }
}
</style>
